package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.domain.Cart;
import com.itheima.reggie.mapper.CartMapper;
import com.itheima.reggie.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class CartServiceImpl implements CartService {
    @Autowired
    private CartMapper cartMapper;
    //添加购物车
    @Override
    public Cart cartAdd(Cart cartParam) {
//先查询是否有此物购物项 条件:用户id+菜品id.用户id+套餐id
        //构建条件
        LambdaQueryWrapper<Cart> cartWrapper = new LambdaQueryWrapper<>();
        cartWrapper.eq(Cart::getUserId, UserHolder.get().getId());//用户id
        cartWrapper.eq(cartParam.getDishId() != null, Cart::getDishId, cartParam.getDishId());//菜品id
        cartWrapper.eq(cartParam.getSetmealId() != null, Cart::getDishId, cartParam.getSetmealId());//套餐id
        //查询记录
        Cart cart = cartMapper.selectOne(cartWrapper);
        //2.判断
        if (cart == null) {//新增记录
            cartParam.setNumber(1);//补齐数量
            cartParam.setUserId(UserHolder.get().getId());//补齐用户id
            cartParam.setCreateTime(new Date());//添加购物车时间
            cartMapper.insert(cartParam);
            //返回增加购物车
            return cartParam;
        } else {
            //数量+1跟新购物车
            cart.setNumber(cart.getNumber() + 1);
            cartMapper.updateById(cart);
            //4.返回更新购物车
            return cart;

        }

    }



    //查询购物车列表
    @Override
    public List<Cart> cartList() {
        //构建条件
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Cart::getUserId, UserHolder.get().getId());//根据当前登录人id
        //查询并返回
        return cartMapper.selectList(wrapper);
    }
//清空购物车
    @Override
    public void cartClean() {
//1.构建条件
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Cart::getUserId, UserHolder.get().getId());
        //2.删除购物车
        cartMapper.delete(wrapper);
    }
//删除购物车
    @Override
    public Cart cartSub(Cart cartParam) {

        /*1.查询需要修改的购物车对象，根据dishId或setmealId 结合用户id
先查询是否有此项购物项 条件：用户id+菜品id，用户id+套餐id*/
        //1.先查询是否有此项购物项 条件：用户id+菜品id，用户id+套餐id
        //1-1 构造条件
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Cart::getUserId, UserHolder.get().getId());
        wrapper.eq(cartParam.getDishId() != null, Cart::getDishId, cartParam.getDishId());
        wrapper.eq(cartParam.getSetmealId() != null, Cart::getSetmealId, cartParam.getSetmealId());
        //1-2 查询记录
        Cart cart = cartMapper.selectOne(wrapper);
        // 2.购物车数量-1
        cart.setNumber(cart.getNumber() - 1);
        //3.判断修改后的数量
        if (cart.getNumber() == 0) {//数量为0清除
            cartMapper.deleteById(cart);
        } else if (cart.getNumber() > 0) {//数量大于0更新数据库
            cartMapper.update(cart, wrapper);
        }
        //4.返回购物车对象
        return cart;

    }
}
